package ru.spb.osll.myfit.client.widget;

import java.util.Vector;

import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.Table;
import ru.spb.osll.myfit.common.TableCell;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;

public class FlexTableWidget extends TableWidget {

	public FlexTableWidget(GWTWidgetFactory gwtWidgetFactory, Table table) {
		super(gwtWidgetFactory, table);
	}

	public PageElement save() {
		Table table = new Table(getTableStyle());
		// table.setFixtureClassName(getFixtureClassName());

		for (int i = 0; i < m_flexTable.getRowCount(); ++i) {
			Vector<String> line = new Vector<String>();
			HorizontalPanel panel = (HorizontalPanel) m_flexTable.getWidget(i, 0);
			
			int widgetCount = panel.getWidgetCount();
			for (int j = 0; j < widgetCount; ++j)
				try {
					String text = ((TextBox) panel.getWidget(j)).getText();
					text = (text.isEmpty()) ? " " : text;
					line.addElement(text);
				} catch (Exception ex) {
				}
				
			if (!line.isEmpty()) {
				Vector<TableCell> lineCell = new Vector<TableCell>();
				
				for (String item : line) {
					TableCell tableCell = new TableCell();
					tableCell.setText(item);
					lineCell.add(tableCell);
				}
				table.addLine(lineCell);
			}
		}
		return table;
	}

	protected void addRow(int row, Vector<TableCell> data) {
		HorizontalPanel line = new HorizontalPanel();
		line.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);

		for (int i = 0; i < data.size(); ++i) {
			TextBox tb = createTextEdit(data.elementAt(i));
			if (i % 2 == 0) {
				// tb.addStyleName("headerCellStyle");
				tb.setReadOnly(true);
			}
			line.add(tb);
			line.setCellHorizontalAlignment(tb,
					HasHorizontalAlignment.ALIGN_LEFT);
		}
		m_flexTable.setWidget(row, 0, line);
	}

	protected void changeWidth(TextBox tb, int displace) {
		if (!tb.isReadOnly()) {
			int length = tb.getText().length() + displace;
			int normLen = (length > 0) ? length : 1;
			tb.setVisibleLength(normLen);
			tb.setText(tb.getText());
		}
	}

	protected void refreshTable(FlexTable table) {
		for (int i = 0; i < table.getRowCount(); ++i)
			if (((HorizontalPanel) table.getWidget(i, 0)).getWidgetCount() == 0)
				table.removeRow(i);
		if (table.getRowCount() == 0){
			m_flexTable.removeFromParent();
		}
	}

	@Override
	protected void addNewRow(int row) {
		m_flexTable.insertRow(row);
		HorizontalPanel line = new HorizontalPanel();
		line.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);

		TextBox tb = createTextEdit(new TableCell());
		line.add(tb);

		m_flexTable.setWidget(row, 0, line);
	}

	@Override
	protected void addNewCell(int row, int cell) {
		// insertCell(row, cell + 1);
		HorizontalPanel line = (HorizontalPanel) m_flexTable.getWidget(row, 0);
		TextBox tb = createTextEdit(new TableCell());
		line.add(tb);
	}

	@Override
	protected void deleteCell(int row, int cell) {
		HorizontalPanel line = (HorizontalPanel) m_flexTable.getWidget(row, 0);
		line.remove(cell);
	}

	@Override
	protected void setReadOnly(boolean isReadOnly) {
	  int rowCount = m_flexTable.getRowCount();
    
	  for (int i = 0; i < rowCount; i++) {
			HorizontalPanel panel = (HorizontalPanel) m_flexTable.getWidget(i, 0);
			for (int j = 0; j < panel.getWidgetCount(); j++)
				try {
					((TextBox) panel.getWidget(j)).setReadOnly(isReadOnly
							|| (j % 2 == 0));
				} catch (Exception ex) {
				}
		}
	}

	protected void tuneMenu() {
		m_deleteRowItem.setVisible(m_selectedRow >= 1);
	}

}
